//
//  LoginVC.m
//  LessoDemo
//
//  Created by decade on 26/12/2017.
//  Copyright © 2017 Lesso. All rights reserved.
//

#import "LoginVC.h"
#import "CRMTextField.h"

@interface LoginVC ()

@property (nonatomic, strong) UIImageView *topLogo;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) CRMTextField *accountInput;
@property (nonatomic, strong) CRMTextField *passwordInput;

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
    [self setupTopLogo];
    [self setupFunctionView];
    [self setupBottomView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView animateWithDuration:1 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CGAffineTransform translation = CGAffineTransformMakeTranslation(0, -SCREEN_HEIGHT*0.089955);
        CGAffineTransform scaleTranslation = CGAffineTransformScale(translation, 0.5, 0.5);
        self.topLogo.transform = scaleTranslation;
        
        self.contentView.frame = CGRectMake(mycontentViewX, mycontentViewY-keyboardFrame.size.height*0.5, mycontentViewWidth, mycontentViewHeight);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification  {
    [UIView animateWithDuration:1 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CGAffineTransform translation = CGAffineTransformMakeTranslation(0, 0);
        CGAffineTransform scaleTranslation = CGAffineTransformScale(translation, 1, 1);
        self.topLogo.transform = scaleTranslation;
        self.contentView.frame = CGRectMake(mycontentViewX, mycontentViewY, mycontentViewWidth, mycontentViewHeight);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)setupTopLogo {
    self.topLogo = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-155, SCREEN_HEIGHT*0.170914, 310, 138)];
    self.topLogo.alpha = 1;
    [self.topLogo setImage:[UIImage imageNamed:@"topLogo"]];
    [self.view addSubview:self.topLogo];
}

- (void)setupFunctionView {
    self.contentView = [[UIView alloc]initWithFrame:CGRectMake(mycontentViewX, mycontentViewY, mycontentViewWidth, mycontentViewHeight)];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.contentView];
    
    self.accountInput = [[CRMTextField alloc]initWithFrame:CGRectMake(0, 0, mycontentViewWidth, mytextFieldHeight) placeHolder:@"Pls enter Account" secureText:NO clearsOnBegin:NO imageNamed:@"accountIcon" customKeyboard:UIKeyboardTypeNumberPad];
    [self.contentView addSubview:self.accountInput];
    
    self.passwordInput = [[CRMTextField alloc]initWithFrame:CGRectMake(0, mytextFieldHeight, mycontentViewWidth, mytextFieldHeight) placeHolder:@"Pls enter Password" secureText:YES clearsOnBegin:NO imageNamed:@"passwordIcon" customKeyboard:UIKeyboardTypeDefault];
    [self.contentView addSubview:self.passwordInput];
    
    UIButton *actionBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.passwordInput.frame.origin.y+self.passwordInput.frame.size.height+19, mycontentViewWidth, 50)];
    actionBtn.backgroundColor = [UIColor orangeColor];
    actionBtn.layer.cornerRadius = 3;
    actionBtn.layer.masksToBounds = YES;
    [actionBtn setTitle:@"Login in" forState:UIControlStateNormal];
    [actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    actionBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [actionBtn addTarget:self action:@selector(doAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:actionBtn];
}

- (void)setupBottomView {
    UILabel *footLB = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT-100, SCREEN_WIDTH-40, 100)];
    footLB.numberOfLines = 0;
    footLB.text = @"account:13800138000 or 123 \n\npassword:123456";
    footLB.textColor = [UIColor lightGrayColor];
    footLB.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:footLB];
    
    UIButton *autoLogin = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-80, SCREEN_HEIGHT-80, 60, 60)];
    autoLogin.backgroundColor = [UIColor blueColor];
    [autoLogin setTitle:@"AutoLogin" forState:UIControlStateNormal];
    autoLogin.titleLabel.font = [UIFont systemFontOfSize:10];
    [autoLogin addTarget:self action:@selector(autoLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:autoLogin];
}

- (void)autoLogin {
    self.accountInput.text = @"13800138000";
    self.passwordInput.text = @"123456";
    [self doAction];
}

- (void)dismissKeyboard {
    if (self.accountInput.isFirstResponder) {
        [self.accountInput resignFirstResponder];
    } else if (self.passwordInput.isFirstResponder) {
        [self.passwordInput resignFirstResponder];
    }
}

- (void)doAction {
    NSLog(@"action click");
    if (!self.accountInput.text || [self.accountInput.text isEqualToString:@""]) {
        [Util showAlertView:@"Account Not Found!"];
        return;
    }
    
    if (!self.passwordInput.text || [self.passwordInput.text isEqualToString:@""]) {
        [Util showAlertView:@"Password is wrong!"];
        return;
    }
    
    for (NSDictionary *tmpDic in testAccount) {
        if ([tmpDic[@"account"] isEqualToString:self.accountInput.text]) {
            if ([tmpDic[@"password"] isEqualToString:self.passwordInput.text]) {
                self.accountInput.text = @"";
                self.passwordInput.text = @"";
                appDelegate.window.rootViewController = appDelegate.navi;
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:ISLOGIN];
                [[NSUserDefaults standardUserDefaults] setObject:tmpDic forKey:CUAccount];
            } else {
                [Util showAlertView:@"Password is wrong!"];
            }
        } else {
            
        }
    }
}

@end
